package br.com.fanor.manager.impl;

import br.com.fanor.listener.MessageListener;
import br.com.fanor.manager.MessageManager;
import java.util.ArrayList;
import java.util.List;

public class MessageManagerImpl implements MessageManager {

    private final List<MessageListener> listeners = new ArrayList<MessageListener>();
    
    @Override
    public void subscribe(final MessageListener listener) {
        if ( listeners.contains(listener) ) {
            throw new IllegalStateException("Já cadastrado!");
        }
        
        listeners.add(listener);
    }

    @Override
    public void unsubscribe(final MessageListener listener) {
        if ( !listeners.contains(listener) ) {
            throw new IllegalStateException("Inscreva-se!");
        }
        
        listeners.remove(listener);
    }
    
    @Override
    public void nofityListeners(final String message) {
        for (MessageListener messageListener : listeners) {
            messageListener.notify(message);
        }
    }

    @Override
    public boolean hasSubscribe(final MessageListener listener) {
        return listeners.contains(listener);
    }
    
}
